package com.cjst.service.impl.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.cjst.pojo.sys.SysDept;
import com.cjst.pojo.sys.SysUser;
import com.cjst.sdp.util.ZTreeNode;
import com.cjst.service.impl.base.BaseServiceImpl;
import com.cjst.service.sys.SysDeptService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.UserPrincipalUtil;
/**
 * 部门服务
 * @author yj
 *
 */
@Service
public class SysDeptServiceImpl extends BaseServiceImpl<SysDept, String> implements SysDeptService{
	/**
	 * 得到部门数
	 */
	@Override
	public List<ZTreeNode> tree(String officeId,SysUser user){
		WherePrams wherePrams=Method.where("status", C.EQ, "1");
		wherePrams.and("officeId", C.EQ, officeId);
		Map<String, Object> authMap = UserPrincipalUtil.getDeptPrincipals(user);
		if("-1".equals(authMap.get("auth"))) {
			return null;
		}else if("2".equals(authMap.get("auth"))){
			wherePrams.andIn("id", C.IN, (List<String>)authMap.get("list"));
		}
		List<SysDept> sysDepList=this.list(wherePrams);
		List<ZTreeNode> nodeList=new ArrayList<ZTreeNode>();
		if(sysDepList!=null && sysDepList.size()>0){
			for (SysDept sysDept : sysDepList) {
				ZTreeNode node=new ZTreeNode();
				node.setId(sysDept.getId());
				node.setpId(sysDept.getPid());
				node.setName(sysDept.getFullname());
				nodeList.add(node);
			}
		}
		return nodeList;
	}
}
